﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using System.Configuration;


namespace MaxStudio.Concrete.Web.Pages
{
    public partial class RPTAshReview : BasePage
    {
        string wholSequence = "LT/S实-12-01";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
            }
        }
        #region Private Method

        private void BindGridView()
        {
            RPTAshReviewService RPTAshReviewServiceobj = ServiceFactory.getRPTAshReviewService();
            OperationResult<List<tb_RPT_AshReview>> result = RPTAshReviewServiceobj.GetAllAshReview();

            if (result.operationSuccess)
            {
                gvAshReview.DataSource = result.obj;
                gvAshReview.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            RPTAshReviewService RPTAshReviewServiceobj = ServiceFactory.getRPTAshReviewService();
            OperationResult<tb_RPT_AshReview> resultEdit = RPTAshReviewServiceobj.GetAshReviewById(id);

            if (resultEdit.operationSuccess == true)
            {
                txtEditProjectName.Text = resultEdit.obj.ProjectName;
                txtEditASHManufacturer.Text = resultEdit.obj.ASHManufacturer;
                txtEditOutSequence.Text = resultEdit.obj.OutSequence;
                txtEditInDate.Text = resultEdit.obj.InDate;
                txtEditTestDate.Text = resultEdit.obj.TestDate;
                txtEditSTANFineness.Text = resultEdit.obj.STANFineness;
                txtEditSTANWaterDemandRatio.Text = resultEdit.obj.STANWaterDemandRatio;
                txtEditSTANIgnitionLoss.Text = resultEdit.obj.STANIgnitionLoss;
                txtEditSTANMoistureContent.Text = resultEdit.obj.STANMoistureContent;
                txtEditSTANSulfurTrioxide.Text = resultEdit.obj.STANSulfurTrioxide;
                txtEditMEASUFineness.Text = resultEdit.obj.MEASUFineness;
                txtEditMEASUWaterDemandRatio.Text = resultEdit.obj.MEASUWaterDemandRatio;
                txtEditMEASUIgnitionLoss.Text = resultEdit.obj.MEASUIgnitionLoss;
                txtEditMEASUMoistureContent.Text = resultEdit.obj.MEASUMoistureContent;
                txtEditMEASUSulfurTrioxide.Text = resultEdit.obj.MEASUSulfurTrioxide;
                txtEditTestStandard.Text = resultEdit.obj.TestStandard; ;
                txtEditTestResult.Text = resultEdit.obj.TestResult;
                txtEditRemark.Text = resultEdit.obj.Remark;
                txtEditReview.Text = resultEdit.obj.Review;
                txtEditApproval.Text = resultEdit.obj.Approval;
                txtEditTest.Text = resultEdit.obj.Test;

                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();
            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        #endregion

        #region Event

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            RPTAshReviewService RPTAshReviewServiceobj = ServiceFactory.getRPTAshReviewService();
            tb_RPT_AshReview instance = new tb_RPT_AshReview();
            instance.Sequence = wholSequence; 
            instance.ProjectName = txtProjectName.Text;
            instance.ASHManufacturer = txtASHManufacturer.Text;
            instance.OutSequence = txtOutSequence.Text;
            instance.InDate = txtInDate.Text;
            instance.TestDate = txtTestDate.Text;
            instance.STANFineness = txtSTANFineness.Text;
            instance.STANWaterDemandRatio = txtSTANWaterDemandRatio.Text;
            instance.STANIgnitionLoss = txtSTANIgnitionLoss.Text;
            instance.STANMoistureContent = txtSTANMoistureContent.Text;
            instance.STANSulfurTrioxide = txtSTANSulfurTrioxide.Text;
            instance.MEASUFineness = txtMEASUFineness.Text;
            instance.MEASUWaterDemandRatio = txtMEASUWaterDemandRatio.Text;
            instance.MEASUIgnitionLoss = txtMEASUIgnitionLoss.Text;
            instance.MEASUMoistureContent = txtMEASUMoistureContent.Text;
            instance.MEASUSulfurTrioxide = txtMEASUSulfurTrioxide.Text; 
            instance.TestStandard = txtTestStandard.Text;
            instance.TestResult = txtTestResult.Text;
            instance.Remark = txtRemark.Text;
            instance.Review = txtReview.Text;
            instance.Approval = txtApproval.Text;
            instance.Test = txtTest.Text;

            OperationResult<tb_RPT_AshReview> result = RPTAshReviewServiceobj.CreateAshReview(instance);
            BindGridView();

        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }

            RPTAshReviewService RPTAshReviewServiceobj = ServiceFactory.getRPTAshReviewService();
            tb_RPT_AshReview entity = new tb_RPT_AshReview();

            entity.Id = editId;
            entity.ProjectName = txtEditProjectName.Text;
            entity.ASHManufacturer =txtEditASHManufacturer.Text;
            entity.OutSequence =txtEditOutSequence.Text;
            entity.InDate =txtEditInDate.Text;
            entity.TestDate =txtEditTestDate.Text;
            entity.STANFineness =txtEditSTANFineness.Text;
            entity.STANWaterDemandRatio =txtEditSTANWaterDemandRatio.Text;
            entity.STANIgnitionLoss =txtEditSTANIgnitionLoss.Text;
            entity.STANMoistureContent =txtEditSTANMoistureContent.Text;
            entity.STANSulfurTrioxide =txtEditSTANSulfurTrioxide.Text;
            entity.MEASUFineness =txtEditMEASUFineness.Text;
            entity.MEASUWaterDemandRatio =txtEditMEASUWaterDemandRatio.Text;
            entity.MEASUIgnitionLoss =txtEditMEASUIgnitionLoss.Text;
            entity.MEASUMoistureContent =txtEditMEASUMoistureContent.Text;
            entity.MEASUSulfurTrioxide =txtEditMEASUSulfurTrioxide.Text; 
            entity.TestStandard = txtEditTestStandard.Text;
            entity.TestResult = txtEditTestResult.Text;
            entity.Remark = txtEditRemark.Text;
            entity.Review = txtEditReview.Text;
            entity.Approval = txtEditApproval.Text;
            entity.Test = txtEditTest.Text;


            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_RPT_AshReview> resultEdit = RPTAshReviewServiceobj.ModifyAshReview(entity);

            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            RPTAshReviewService RPTAshReviewServiceobj = ServiceFactory.getRPTAshReviewService();
            tb_RPT_AshReview deleteEntity = new tb_RPT_AshReview();

            deleteEntity.Id = deleteId;
            RPTAshReviewServiceobj.DeleteAshReview(deleteEntity);

            BindGridView();
        }

        protected void gvAshReview_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvAshReview_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAshReview.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        #endregion
    }
}
